package com.lyh.hkcloud.activity;

import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.*;
import androidx.appcompat.app.ActionBar;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.util.base.LoadingUtils;
import com.lyh.hkcloud.util.base.LogUtil;
import com.lyh.hkcloud.util.base.ToastUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

public class WebActivity extends BaseActivity {

    private WebView wvWeb;

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {
        wvWeb = findViewById(R.id.wv_web);
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onAfterInit() {
        initPage();
    }

    private void initPage() {
        LoadingUtils.getInstance(com.lyh.hkcloud.activity.WebActivity.this).show();
        JSONObject object = null;
        try {
            object = new JSONObject(getIntent().getStringExtra("object"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ActionBar actionBar = this.getSupportActionBar();
        assert actionBar != null;
        actionBar.setTitle(object.optString("name"));
        actionBar.setDisplayHomeAsUpEnabled(true);
        String url = object.optString("sysFunTransactionURL");
        url += (url.indexOf("?") > 0 ? "&" : "?") + "randomId=" + System.currentTimeMillis();
        LogUtil.i("url------", url);
        wvWeb.loadUrl(url);
        wvWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http")) {
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                LoadingUtils.getInstance(com.lyh.hkcloud.activity.WebActivity.this).hide();
                super.onPageFinished(view, url);
            }
        });
        //声明WebSettings子类

        WebSettings webSettings = wvWeb.getSettings();

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webSettings.setBuiltInZoomControls(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //关闭webview中缓存
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSettings.setJavaScriptEnabled(true);
        wvWeb.addJavascriptInterface(this, "java");
    }


    @Override
    @JavascriptInterface
    public void alert(String msg) {
        Message message = new Message();
        message.what = 0;
        message.getData().putString("msg", msg);
        handler.sendMessage(message);
    }

    @JavascriptInterface
    public void back() {
        onKeyDown(KeyEvent.KEYCODE_BACK, null);
    }

    private void callJs() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            wvWeb.evaluateJavascript("javascript:document.body.innerText", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    LogUtil.i("js--------", value);
                    ToastUtils.makeText(com.lyh.hkcloud.activity.WebActivity.this, value, ToastUtils.LENGTH_SHORT).show();
                }
            });
        }
    }


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {//定义一个Handler，用于处理下载线程与UI间通讯
            if (!Thread.currentThread().isInterrupted()) {
                switch (msg.what) {
                    case 0:
                        ToastUtils.makeText(com.lyh.hkcloud.activity.WebActivity.this, msg.getData().getString("msg"), ToastUtils.LENGTH_SHORT).show();
                }
            }
            return false;
        }
    });
}
